package com.tencent.could.ocrdemo.utils;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Environment;
import android.util.Log;

import com.ahrykj.weddingcartaxi.R;
import com.tencent.could.ocrdemo.demo.App;
import com.tencent.ocr.sdk.common.CustomConfigUi;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 自定义UI的配置
 *
 * @since 2020/7/24
 */
public class CustomConfigUtil {

    private CustomConfigUi customConfigUi;

    /**
     * 私有构造方法
     */
    private CustomConfigUtil() {
        initConfig();
    }

    /**
     * 静态内部类
     */
    private static final class CustomConfigUtilHolder {
        private static final CustomConfigUtil INSTANCE = new CustomConfigUtil();
    }

    /**
     * 单例方法
     *
     * @return 单例对象
     */
    public static CustomConfigUtil getInstance() {
        return CustomConfigUtilHolder.INSTANCE;
    }

    /**
     * 初始化相关信息
     */
    private void initConfig() {
        customConfigUi = new CustomConfigUi();
        customConfigUi.setTitleBarText("测试识别信息");
        customConfigUi.setShowTitleBar(false);
        customConfigUi.setRemindDialogText("这里是一段提示文字信息");
        // 测试颜色修改
        int testColor = App.getApp().getResources().getColor(R.color.pale_green);
        customConfigUi.setTitleColor(testColor);
        customConfigUi.setCardFrameColor(testColor);
        customConfigUi.setRemindConfirmColor(testColor);
        customConfigUi.setSuccessRemindTextColor(testColor);
        // 更新可以自定义的图片样式
        customConfigUi.setLightImageResId(R.drawable.config_ocr_light_on, R.drawable.config_ocr_light_off);
        customConfigUi.setTakePicturesResId(R.drawable.config_ocr_take_pictures);
        customConfigUi.setImageSelectResId(R.drawable.config_ocr_photo_album);
    }

    /**
     * 获取当前的UI配置
     *
     * @return 当前的ui配置
     */
    public CustomConfigUi getCustomConfigUi() {
        return customConfigUi;
    }


    public Bitmap sharpenImageAmeliorate(Bitmap bmp) {
        long start = System.currentTimeMillis();
        // 拉普拉斯矩阵
        int[] laplacian = new int[]{-1, -1, -1, -1, 9, -1, -1, -1, -1};
        //        int[] laplacian = new int[]{0, -1, 0, -1, 5, -1, 0, -1, 0};
        //        int[] laplacian = new int[]{1, -2, 1, -2, 5, -2, 1, -2, 1};
        int width = bmp.getWidth();
        int height = bmp.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

        int pixR = 0;
        int pixG = 0;
        int pixB = 0;

        int pixColor = 0;

        int newR = 0;
        int newG = 0;
        int newB = 0;

        int idx = 0;
        float alpha = 1F;
        //原图像素点数组
        int[] pixels = new int[width * height];
        //创建一个新数据保存锐化后的像素点
        int[] pixels_1 = new int[width * height];
        bmp.getPixels(pixels, 0, width, 0, 0, width, height);
        for (int i = 1, length = height - 1; i < length; i++) {
            for (int k = 1, len = width - 1; k < len; k++) {
                idx = 0;
                for (int m = -1; m <= 1; m++) {
                    for (int n = -1; n <= 1; n++) {
                        pixColor = pixels[(i + n) * width + k + m];
                        pixR = Color.red(pixColor);
                        pixG = Color.green(pixColor);
                        pixB = Color.blue(pixColor);

                        newR = newR + (int) (pixR * laplacian[idx] * alpha);
                        newG = newG + (int) (pixG * laplacian[idx] * alpha);
                        newB = newB + (int) (pixB * laplacian[idx] * alpha);
                        idx++;
                    }
                }

                newR = Math.min(255, Math.max(0, newR));
                newG = Math.min(255, Math.max(0, newG));
                newB = Math.min(255, Math.max(0, newB));

                pixels_1[i * width + k] = Color.argb(255, newR, newG, newB);
                newR = 0;
                newG = 0;
                newB = 0;
            }
        }

        bitmap.setPixels(pixels_1, 0, width, 0, 0, width, height);
        long end = System.currentTimeMillis();
        Log.d("may", "used time=" + (end - start));
        return bitmap;
    }

    public String saveImage(Bitmap bmp) {

        File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);

        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }
}
